import SDKBase from "./SDKBase";
import { SDKState } from "../SDKDefine";
export default class SDKRecorder extends SDKBase {
    private static ins: SDKRecorder

    static instance() {
        if (!this.ins) {
            this.ins = new SDKRecorder();
        }
        return this.ins;
    }
    protected recorder: any;

    protected videoPath: string = null;

    protected isSave: boolean = true;

    private startTime: number = 0;

    protected _state: SDKState = SDKState.CLOSE;

    protected callback: (r: number) => void = null;
    constructor() {
        super();
        if (this.sdk) {
            this.recorder = this.sdk.getGameRecorderManager();
            if (this.recorder) {
                this.recorder.onStart(this.bind(this.onStart))
                this.recorder.onStop(this.bind(this.onStop))
                this.recorder.onResume(this.bind(this.onResume))
                this.recorder.onPause(this.bind(this.onPause))
                this.recorder.onError(this.bind(this.onError))
            }
        }


    }



    onStart(res: any) {
        console.log('录屏开始', res);
        this.setState(SDKState.OPEN)
        // do somethine;
    }
    onError(errMsg: any) {
        console.log('TTRecorder onPuase');
        this.videoPath = null;
        this.sdk.showToast('视频制作失败:' + errMsg)
    }
    onPause() {
        console.log('TTRecorder onPuase');

    }

    onResume() {
        console.log('TTRecorder onResume');
    }


    onStop(res: any) {
        // console.log(res.videoPath);
        // do somethine;
        // this.channel.showToast('视频制作中')
        let self = this;
        console.log('录屏结束', res);
        if (self.startTime <= 5) {
            if (self.callback) {
                self.callback(SDKState.FAIL);
                self.callback = null;
            }
            self.setState(SDKState.CLOSE)
            self.sdk.showToast('录屏时间太短')
        } else {
            if (self.startTime > 30) {
                self.startTime = 30;
            }

            self.recorder.clipVideo({
                path: res.videoPath,
                timeRange: [self.startTime, 0],
                success(r: any) {
                    console.log(r.videoPath); // 生成最后10秒的视频
                    self.videoPath = r.videoPath
                    if (self.callback) {
                        self.callback(SDKState.SUCCESS);
                        self.callback = null;
                    }
                    self.setState(SDKState.CLOSE)
                },
                fail(e: any) {
                    self.videoPath = null;
                    if (self.callback) {
                        self.callback(SDKState.FAIL);
                        self.callback = null;
                    }
                    self.setState(SDKState.CLOSE)
                    // console.error(e);
                },


            })
        }
    }


    setState(s: SDKState) {
        this._state = s;
        // this.emit(this.state)
    }

    isMaking() {
        return this._state == SDKState.LOADING;
    }

    start() {
        if (this.isOpen() || this.isMaking()) {
            return;
        }
        this.startTime = Date.now()
        // this.changeState(ItemState.GOT)
        this.recorder.start({
            duration: 300,
        })
    }


    stop(isSave: boolean = true, callback: (r: number) => void) {
        if (this.isClose() || this.isMaking()) {
            return;
        }
        this.startTime = Math.floor((Date.now() - this.startTime) / 1000)
        this.callback = callback;
        // this.changeState(ItemState.NOT_GET)
        this.isSave = isSave;
        this._state = SDKState.LOADING;
        this.recorder.stop();
    }

    pause() {
        this.recorder.pause();
    }

    resume() {
        this.recorder.resume();
    }

    getVideoPath() {
        return this.videoPath;
    }

    clear() {
        this.videoPath = null;
    }

    isOpen() {
        return this._state == SDKState.OPEN;
    }

    isClose() {
        return this._state == SDKState.CLOSE;
    }

    getState() {
        return this._state;
    }
}
